<template>
  <el-card class="detail-card">
    <el-tabs v-model="activeName">
      <!-- 登录账户设置 -->
      <el-tab-pane label="登录账户设置" name="loginInfo">
        <el-form
          ref="baseForm"
          :model="userInfo"
          :rules="rules"
          label-width="100px"
          style="width:450px; margin:50px auto"
        >
          <el-form-item label="姓名：" prop="username">
            <el-input type="text" v-model="userInfo.username"></el-input>
          </el-form-item>

          <el-form-item label="密码：" prop="password">
            <el-input v-model="userInfo.password" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save">确定</el-button>
            <el-button @click="$router.push('/employees')">取消</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="个人详情" name="userInfo">
        <user-info />
      </el-tab-pane>
      <el-tab-pane label="岗位信息" name="jobInfo">
        <job-info />
      </el-tab-pane>
    </el-tabs>
  </el-card>
</template>

<script>
// 导入组件
import userInfo from './components/user-info.vue'
import jobInfo from './components/job-info.vue'
// 导入接口
import { getUserBasicInfo } from '@/api/user'
import { saveProfile } from '@/api/employees'
export default {
  components: {
    userInfo,
    jobInfo
  },
  data () {
    return {
      // 默认选中的tabs
      activeName: 'loginInfo',
      userInfo: {}, // 登录账户设置里的用户信息
      rules: {
        username: [
          {
            required: true,
            message: '姓名不能为空',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '姓名不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    // 获取用户基本资料
    this.getBaseProfile()
  },

  methods: {
    //   获取用户基本资料
    async getBaseProfile () {
      let res = await getUserBasicInfo(this.$route.query.id)
      //   console.log(res)
      this.userInfo = res.data
    },
    // 更新用户信息
    save () {
      // 表单的统一验证
      this.$refs.baseForm.validate(async v => {
        if (v) {
          // 通过验证
          //   发送请求 修改信息
          await saveProfile(this.userInfo)
          //   弹窗提示
          this.$message.success('修改成功！')
          //   跳转回员工页
          this.$router.push('/employees')
        } else {
          // 没有通过验证
          this.$message.error('写点什么吧...')
        }
      })
    }
  }
}
</script>

<style>
.detail-card {
  margin: 50px;
}
@page {
  margin: 3mm;
}
</style>
